// plugin/components/sentence-box/index.js
const plugin = require('../../index');
Component({
  behaviors: ['wx://component-export'],
  export () {
    return this
  },
  /**
   * 组件的属性列表
   */
  properties: {
    word: {
      type: String,
      observer: function (newVal, oldVal) {
        //侦听器监听单词信息变化
        let word = newVal;
        console.log(word)
        if (word.length >= 4) {
          this.reqSentence(word);
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    sentenceList: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    async reqSentence(word) {
      var that = this;
      let sentenceRes = await plugin.sentenceQuery(word);
      console.log(sentenceRes)
      let sentenceList = sentenceRes.data;
      // sentenceList = sentenceList.concat(sentenceList)
      /** 例句转接 */
      sentenceList.forEach(function (ele, index) {
        let tarr = ele.sentence.split(that.properties.word, 2)

        ele.first = tarr[0]
        ele.mid = that.properties.word
        ele.last = tarr[1]
      })
      this.setData({
        sentenceList
      })
    }
  }
})